package com.yunhe.uitl;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class C3p0Test {
    static ClassPathXmlApplicationContext cpac;
    static {
        cpac=new ClassPathXmlApplicationContext("spring.xml");
    }
    @Test
    public void c() throws Exception {
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }

    @Test
    public void spring_c3p0() throws SQLException {
        //DataSource是java提供的数据源接口
        //所有的连接池必须实现该接口 所以在开发中
        //通过声明DataSource数据源对象 保存对应的连接池是数据源对象
        DataSource ds = (DataSource) cpac.getBean("c3p0");
        Connection connection = ds.getConnection();
        System.out.println(connection);
    }


}
